home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / snws190s.zip / UNBATCH.C < prev    next >
C/C++ Source or Header  |  1992-06-21  |  11KB  |  377 lines

  1. /*
  2.     SNEWS 1.12
  3.  
  4.     unbatch - quick and dirty news toss, no feeding of other sites
  5.  
  6.  
  7.     Copyright (C) 1991  John McCombs, Christchurch, NEW ZEALAND
  8.                         john@ahuriri.gen.nz
  9.                         PO Box 2708, Christchurch, NEW ZEALAND
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License, version 1, as
  13.     published by the Free Software Foundation.
  14.  
  15.     This program is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     See the file COPYING, which contains a copy of the GNU General
  21.     Public License.
  22.  
  23.  
  24.  
  25.     USAGE: unbatch
  26.             -n  means decompress the first batch, then stop before tossing
  27.             -v  means verbose, tell what we are doing
  28.  
  29.  */
  30.  
  31.  
  32. #include "defs.h"
  33. #include "unbatch.h"
  34.  
  35. #define BUFFSIZE 16000
  36.  
  37. unsigned _stklen = 16384;
  38. char *inbuf  = NULL;
  39. char *outbuf = NULL;
  40.  
  41. INFO my_stuff;
  42.  
  43.              
  44. /*------------------------------- main --------------------------------*/
  45. void main(int argc, char *argv[])
  46. {
  47.     FILE   *tmp_file;
  48.     static char name[256], in_name[256];
  49.     int    done, no_toss;
  50.     int    drive;
  51.     long   required_disk, disk_free;
  52.     struct stat st;
  53.     struct dfree df;
  54.     struct ffblk ffblk;
  55.  
  56.     fprintf(stderr, "UNBATCH: (%s)\n", VERSION);
  57.  
  58.     /* if TRUE, then just uncompress, don't unbatch */
  59.  
  60.     /*
  61.     inbuf  = malloc(BUFFSIZE);
  62.     outbuf = malloc(BUFFSIZE);
  63.  
  64.     if (!inbuf || !outbuf) {
  65.         fprintf(stderr, "not enough memory to set file buffers");
  66.         exit(-1);
  67.     } */
  68.  
  69.     no_toss = (argc == 2) && (strncmp("-n", argv[1], 2) == 0);
  70.  
  71.     if (!load_stuff()) {
  72.         fprintf(stderr, "Couldn't read rc info\n");
  73.         exit(1);
  74.     }
  75.  
  76.     load_active_file();
  77.     free_ng();
  78.  
  79.     sprintf(in_name, "%s*.*", my_stuff.incoming_dir);
  80.     done = findfirst(in_name, &ffblk, 0);
  81.     while (!done) {
  82.  
  83.         sprintf(name, "%s%s", my_stuff.incoming_dir, ffblk.ff_name);
  84.         printf("unbatch: processing %s\n", name);
  85.  
  86.         /*
  87.          *  Check for enough room.  We need:
  88.          *
  89.          *     - more than twice the size of the batch for uncompressed
  90.          *       batch, say 2.8
  91.          *
  92.          *     - space for the articles.
  93.          *
  94.          *       For example a 100k batch may require 280kb for the uncompressed
  95.          *       batch, then another 280 kb for the articles.
  96.          */
  97. /*
  98.         stat(name, &st);
  99.  
  100.         required_disk = (st.st_size*2.8) * 2;
  101.  
  102.         drive = getdisk();
  103.         getdfree(drive+1, &df);
  104.         disk_free = (long)df.df_avail * (long)df.df_bsec * (long)df.df_sclus;
  105.  
  106.         if (disk_free < required_disk){
  107.             fprintf(stderr, "unbatch: %ld bytes of disk req'd to unpack batch\n");
  108.             exit(1);
  109.         }
  110. */
  111.  
  112.         /* uncompress the news batch and return a pointer to the temp file */
  113.         if ((tmp_file = decode_batches(name, no_toss)) != NULL) {
  114.  
  115.             open_hist_file();
  116.             toss(tmp_file);
  117.             close_hist_file();
  118.             fclose(tmp_file);
  119.  
  120.             unlink(name);
  121.             sprintf(name, "%s", my_stuff.temp_name);
  122.             unlink(name);
  123.  
  124.         } else {
  125.             printf("could not unpack compressed news batch %s\n", name);
  126.             exit(1);
  127.         }
  128.  
  129.         done = findnext(&ffblk);
  130.  
  131.     }
  132.  
  133.     close_active_file();
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. /*--------------------------- unpack the batch ------------------------*/
  143. void toss(FILE *tmp_file)
  144. {
  145.     /*
  146.      *  Toss it into the appropriate files.
  147.      *
  148.      *  TODO:
  149.      *    - badly structured, sort it out
  150.      */
  151.  
  152.     FILE *out_file = NULL;
  153.     FILE *spool_file = NULL;
  154.     char buf[512], msg[256], subject[256], msg_id[256], *p;
  155.     char junk_group[] = "junk";
  156.     char newsgroups[256], nglist[256];
  157.     long where;
  158.     ACTIVE *gp;
  159.     time_t t;
  160.     int  already_junked, in_header, posted;
  161.  
  162.     strcpy(msg_id, "");
  163.     strcpy(subject, "-- no subject --\n");
  164.     strcpy(newsgroups, "");
  165.  
  166.     rewind(tmp_file);
  167.     time(&t);
  168.     in_header = TRUE;
  169.  
  170.     /* read the file */
  171.     while (fgets(buf, 511, tmp_file) != NULL) {
  172.  
  173.         if (strstr(buf, "#! rnews") != NULL) {
  174.  
  175.             if (strlen(newsgroups) != 0) {
  176.                 strcpy(nglist,  newsgroups+11);
  177.                 p = strtok(newsgroups, " \r\n,:");
  178.                 p = strtok(NULL, " \r\n,:");
  179.  
  180.                 /*
  181.                  *  For each newsgroup
  182.                  *    - open the text file
  183.                  *    - save the file pointer
  184.                  *    - append the message to it
  185.                  *    - close the file
  186.                  *    - open the index file
  187.                  *    - save the file pointer and the subject line
  188.                  */
  189.                 posted = 0;
  190.                 while (p != '\x00') {
  191.  
  192.                     out_file = open_out_file(p);
  193.                     if (out_file) {
  194.                         posted = 1;
  195.                         where = ftell(out_file);
  196.                         rewind(spool_file);
  197.                         while (fgets(buf, 511, spool_file) != NULL)
  198.                             fputs(buf, out_file);
  199.                         fprintf(out_file, "\n@@@@END\n");
  200.                         fclose(out_file);
  201.  
  202.                         out_file = open_index_file(p);
  203.                         gp = find_news_group(p);
  204.                         fprintf(out_file,"%08ld %08ld %09ld %s", where,
  205.                             gp->hi_num, t, subject);
  206.                         fclose(out_file);
  207.                     }
  208.                     p = strtok(NULL, " \r\n,:");
  209.                 }
  210.                 if (!posted) {
  211.                     out_file = open_out_file(junk_group);
  212.                     if (out_file) {
  213.                         where = ftell(out_file);
  214.                         rewind(spool_file);
  215.                         while (fgets(buf, 511, spool_file) != NULL)
  216.                             fputs(buf, out_file);
  217.                         fprintf(out_file, "\n@@@@END\n");
  218.                         fclose(out_file);
  219.  
  220.                         out_file = open_index_file(junk_group);
  221.                         gp = find_news_group(junk_group);
  222.                         fprintf(out_file,"%08ld %08ld %09ld %s", where,
  223.                             gp->hi_num, t, subject);
  224.                         fclose(out_file);
  225.                     }
  226.                     else {
  227.                         fprintf(stderr,"\njunk group *must* exist!");
  228.                         exit(-1);
  229.                     }
  230.                 }
  231.                 add_hist_record(msg_id, nglist);
  232.                 fclose(spool_file);
  233.             }
  234.  
  235.             strcpy(subject, "-- no subject --\n");
  236.             in_header = TRUE;
  237.  
  238.             if ((spool_file = fopen("$$item", "w+b")) == NULL) {
  239.                 fprintf(stderr, "can't open temp article file %s\n", "$$item");
  240.                 exit(1);
  241.             }
  242.  
  243.         } else {
  244.             /* flag the end of the header */
  245.             if (strcmp(buf, "\n") == 0)
  246.                 in_header = FALSE;
  247.  
  248.             /* save the newsgroups line */
  249.             if (in_header) {
  250.                 if (strnicmp(buf,"Message-ID:",11) == 0) {
  251.                     strcpy(msg, buf);
  252.                     p = strtok(msg, " \t\r\n");  p = strtok(NULL, " \t\r\n");
  253.                     strcpy(msg_id, p);
  254.                 }
  255.                 if (strnicmp(buf,"Newsgroups:",11) == 0) strcpy(newsgroups,buf);
  256.                 if (strnicmp(buf,"Subject:",8) == 0) {
  257.                     strcpy(subject, buf+8);
  258.                 }
  259.             }
  260.             /* add our system name to the path list */
  261.             if (strnicmp(buf, "Path:", 5) == 0) {
  262.                 p = strtok(buf, " \t");
  263.                 p = strtok(NULL, " \t");
  264.                 fprintf(spool_file, "Path: %s!%s", my_stuff.my_site, p);
  265.             } else {
  266.                 fprintf(spool_file, "%s", buf);
  267.             }
  268.         }
  269.  
  270.     }
  271.  
  272.     /* process the last one */
  273.     if (strlen(newsgroups) != 0) {
  274.         strcpy(nglist, newsgroups+11);
  275.         p = strtok(newsgroups, " \r\n,:");
  276.         p = strtok(NULL, " \r\n,:");
  277.         posted = 0;
  278.         while (p != '\x00') {
  279.             out_file = open_out_file(p);
  280.             if (out_file) {
  281.                 posted = 1;
  282.                 where = ftell(out_file);
  283.                 rewind(spool_file);
  284.                 while (fgets(buf, 511, spool_file) != NULL)
  285.                     fputs(buf, out_file);
  286.                 fprintf(out_file, "\n@@@@END\n");
  287.                 fclose(out_file);
  288.  
  289.                 out_file = open_index_file(p);
  290.                 gp = find_news_group(p);
  291.                 fprintf(out_file,"%08ld %08ld %09ld %s", where, gp->hi_num, t, subject);
  292.                 fclose(out_file);
  293.             }
  294.             p = strtok(NULL, " \r\n,:");
  295.         }
  296.         if (!posted) {
  297.             out_file = open_out_file(junk_group);
  298.             if (out_file) {
  299.                 where = ftell(out_file);
  300.                 rewind(spool_file);
  301.                 while (fgets(buf, 511, spool_file) != NULL)
  302.                     fputs(buf, out_file);
  303.                 fprintf(out_file, "\n@@@@END\n");
  304.                 fclose(out_file);
  305.  
  306.                 out_file = open_index_file(junk_group);
  307.                 gp = find_news_group(junk_group);
  308.                 fprintf(out_file,"%08ld %08ld %09ld %s", where, gp->hi_num, t, subject);
  309.                 fclose(out_file);
  310.             }
  311.         }
  312.         add_hist_record(msg_id, nglist);
  313.         fclose(spool_file);
  314.         unlink("$$item");
  315.     }
  316. }
  317.  
  318.  
  319.  
  320.  
  321.  
  322. /*--------------------------- unpack the batch ------------------------*/
  323. FILE *decode_batches(char *fn, int no_toss)
  324. {
  325.     /*
  326.      *  take the batch, strip off the !cunbatch, and feed the file to
  327.      *  uncompress, the opened uncompressed file is returned
  328.      */
  329.  
  330.     FILE *in_file, *out_file;
  331.     int res, buf_cnt;
  332.     char buf[1025];
  333.     char name[128];
  334.  
  335.     buf_cnt = 0;
  336.  
  337.     /* open the batch file */
  338.     strcpy(name, fn);
  339.     if ((in_file = fopen(name, "rb")) == NULL) {
  340.         printf("batchfile %s could not be opened\n", name);
  341.         return(NULL);
  342.     }
  343.  
  344.     /* open the compressed */
  345.     sprintf(name, "%s.z", my_stuff.temp_name);
  346.     if ((out_file = fopen(name, "wb")) == NULL) {
  347.         printf("cannot open output file %s\n", name);
  348.         return(NULL);
  349.     }
  350.  
  351.     while ((res = fread(buf, 1, 1024, in_file)) > 0) {
  352.         fwrite(buf_cnt == 0 ? buf+12 : buf, 1,
  353.                buf_cnt == 0 ? res-12 : res, out_file);
  354.         buf_cnt++;
  355.     }
  356.  
  357.     fclose(in_file);
  358.     fclose(out_file);
  359.  
  360.     unlink(my_stuff.temp_name);
  361.     sprintf(name, "compress -d %s.z", my_stuff.temp_name);
  362.     /* printf("unbatch: decompressing batch (%s)\n", name); */
  363.     system(name);
  364.  
  365.     if (no_toss) return(NULL);
  366.  
  367.     /* open the uncompressed */
  368.     sprintf(name, "%s", my_stuff.temp_name);
  369.     if ((out_file = fopen(name, "rb")) == NULL) {
  370.         printf("cannot open uncompressed file %s\n", name);
  371.         return(NULL);
  372.     }
  373.  
  374.     return(out_file);
  375.  
  376. }
  377.